package com.flightgame.hibernate;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class ItemDao {
	@Autowired
	private SessionFactory sessionFactory;
	
	@Transactional
	public void addItem(Item item){
		try{
			sessionFactory.getCurrentSession().persist(item);
			}
		catch(Exception sq){
			sq.printStackTrace();
		}
	}
	
    @SuppressWarnings("unchecked")
	public List<Item> getItems(){
		return sessionFactory.getCurrentSession().createQuery("from Item").list();
	}
    
    public Item getItem(String itemName){
    	return (Item) sessionFactory.getCurrentSession().createQuery("from Item where itemname=:itemname").setParameter("itemname",itemName).uniqueResult();
    }
}
